package org.catdroid.englishformusicians;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

class AdapterTestScreen extends ArrayAdapter {
	Activity a = null;
	List<Sentence> sentences = null;

	public AdapterTestScreen(Activity a, List<Sentence> sentences) {
		super(a, R.layout.row_test, sentences);
		this.a = a;
		this.sentences = sentences;
	}

	public View getView(int pos, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null || !(convertView.getTag() instanceof ViewHolder)) {
			LayoutInflater mInflater = LayoutInflater.from(a);
			convertView = mInflater.inflate(R.layout.row_test, null);
			// Creates a ViewHolder and store references to the two children views we want to bind data to.
			holder = new ViewHolder();
			holder.linia1 = (TextView) convertView.findViewById(R.id.linia1);
			holder.linia2 = (TextView) convertView.findViewById(R.id.linia2);
			holder.check = (CheckBox) convertView.findViewById(R.id.check);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final Sentence sentence = sentences.get(pos);

		holder.linia1.setText(sentence.textoVO);
		holder.linia2.setText(sentence.textoTraducido);
		holder.check.setChecked(TestRepository.isChecked(a, MainActivity.cleanSentenceString(sentence.textoVO)));

		convertView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				MainActivity.lastSentence = sentence.textoVO;
				GoogleVoiceUtil.launchGoogleVoice(a, sentence.textoVO);
			}
		});

		return convertView;
	}

	private class ViewHolder {
		TextView linia1;
		TextView linia2;
		CheckBox check;
	}
}